#!/bin/sh /etc/rc.common
# Copyright (C) 2006 OpenWrt.org

START=50
USE_PROCD=1

parse_device() {
	local cfg="$1"

	local enabled
	config_get_bool enabled "$cfg" "enabled" "1"
	[ "$enabled" -eq "1" ] || return 1
	

	local ip download upload comment mac
	config_get ip "$cfg" ip
	config_get download "$cfg" "download"
	config_get upload "$cfg" "upload"
	config_get comment "$cfg" "comment"
	config_get mac "$cfg" "mac"

	eqos add "$ip" "$download" "$upload" "$comment" "$mac"
}

start_service() {
	config_load "eqos"

	local enabled
	config_get_bool enabled "config" "enabled" "0"
	[ "$enabled" -eq "1" ] || return 1
	
	local download upload comment
	config_get download "config" "download"
	config_get upload "config" "upload"
	config_get comment "config" "comment"
	
	eqos start "$download" "$upload" "$comment"
	
	local ipv6enabled                              
        config_get_bool ipv6enabled "config" "ipv6enabled" "0"    
        ip6tables -t mangle -N eqos
        ip6tables -t mangle -F eqos
        ebtables -t nat  -N eqos
        ebtables -t nat  -F eqos
        if [ "$ipv6enabled" -eq "1" ]; then                                
        ebtables -t nat -D POSTROUTING  -j eqos -p ipv6              
        ebtables -t nat -A POSTROUTING  -j eqos -p ipv6
        ip6tables -t mangle -D FORWARD  -j eqos
        ip6tables -t mangle -A FORWARD  -j eqos             
        fi  
        config_foreach parse_device "device"
}

stop_service() {
	eqos stop
}

reload_service() {
	stop
	start
}

service_triggers() {
	procd_add_reload_trigger "eqos"
}
